<?php
namespace app\pay\controller\juyin;

class Unionpay extends Base
{
    /**
 * [聚银]支付接口中转
 * ================================================================
 * order 银联快捷入口，方法中转
 * ================================================================
 */
    public function test() {
        return ('123');
    }
    
    // 银联快捷下单入口 
    public function order() {
        $data = input('post.');     // 获取客户端POST过来的参数
        // 验证签名
        $res = $this->verifySign($data, false);
        if($res) {
            $this->request();
            // 组装POST到聚银的数据
            $postToJuyin = array(
                'merchant_id'     => $this->cfg->C('merchantId', false), // 更改商户号为聚银商户号
                'pay_orderid'     => $data['pay_orderid'],
                'pay_amount'      => $data['pay_amount'],
                'pay_callbackurl' => $this->cfg->C('callbackUrl', false),
                'pay_turnyurl'    => $data['pay_turnyurl'],
                'pay_tradetype'   => $data['pay_tradetype'],
                'pay_userid'      => $data['pay_userid'],
                'pay_productname' => $data['pay_productname'],
                'clientIp'        => $data['clientIp'],
            );
            
            /*
             * 1.将订单信息保存到数据库中
             * 2.验证聚银签名并向聚银官方接口发起POST请求
             */
            $ret = $this->requestData($postToJuyin, 'unionpay', false);
            // 接收聚银返回的数据并组装
            $result = $this->responseData($data['merchant_id'], $ret);
            $retData = json_decode($ret, true);
            if($retData['code'] == '11') {
                // 将订单信息保存到数据库中
                $this->payBisOrderAdd($data);
            }
            return $result;
        } else {
            return $res;
        }
    }

    // 接收聚银返回的数据并组装
    public function responseData($merchantId, $data) {
        $retData = json_decode($data, true);
        if($retData['code'] == '11') {
            // 数据组装
            $result = array(
                'code'      => $retData['code'],
                'msg'       => $retData['msg'],
                'orderId'   => $retData['orderId'],
                'notifyUrl' => $retData['notifyUrl'],
                'memberid'  => $merchantId,
                'orderDate' => $retData['orderDate'],
                'requestNo' => $retData['requestNo'],
                'transAmt'  => $retData['transAmt'],
                'codeUrl'   => $retData['codeUrl'],
                'type'      => $retData['type'],
                'turnyurl'  => $retData['turnyurl'],
            );
            $this->request();
            // 生成新的签名
            $newJuyinSignVal = $this->payRequest->createJuyinSign($result, false);
            $result['signature'] = $newJuyinSignVal;
        } else {
            $result = array(
                'code' => $retData['code'],
                'msg'  => $retData['msg']
            );
        }
        return json_encode($result, true);
    }
    
    // 订单信息添加
    public function payBisOrderAdd($data) {
        $orderInfo = array(
            'pay_orderid'     => $data['pay_orderid'],
            'merchant_id'     => $data['merchant_id'],
            'pay_amount'      => $data['pay_amount'],
            'actual_amount'   => $data['pay_amount'] - $data['pay_amount'] / 100,
            'pay_tradetype'   => $data['pay_tradetype'],
            'payment_time'    => null,
            'pay_callbackurl' => $data['pay_callbackurl'],
        );
        model('PayBisOrder')->add($orderInfo);
    }
}

